﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Inovout.Models
{
    public abstract class DataStructuralObject : DataObject
    {
        public abstract string Name { get; set; }

        public abstract IEnumerable<DataStructuralPropertyObject> DataStructuralProperties { get; }

        /// <summary>
        /// 生成强类型结构
        /// </summary>
        /// <param name="baseType">父类</param>
        /// <param name="nameSpace">命名空间</param>
        /// <returns>强类型结构</returns>
        public virtual Type GetClrType(Type baseType, string nameSpace = "")
        {
            string className = this.Name;
            if (String.IsNullOrEmpty(nameSpace))
            {
                nameSpace = "";
            }
            else
            {
                className = nameSpace + "." + this.Name;
            }

            if (baseType == null)
            {
                baseType = typeof(System.Object);
            }

            IDictionary<string, Type> fields = null;

            if (DataStructuralProperties != null)
            {
                fields = DataStructuralProperties.ToDictionary(x => x.Name, x => x.Type);
            }

            return DynamicTypeBuilder.CompileDocumentType(className, fields, baseType, nameSpace);
        }
    }
}
